<?php

namespace common\models\shop;

use common\components\Upload;
use common\models\business\BusinessPig;
use Yii;
use yii\helpers\Url;

/**
 * This is the model class for table "shop_goods".
 *
 * @property string $id
 * @property string $cateid
 * @property integer $status
 * @property string $displayorder
 * @property string $title
 * @property string $thumb
 * @property string $mutithumb
 * @property string $unit
 * @property string $description
 * @property string $content
 * @property string $goodssn
 * @property string $marketprice
 * @property string $productprice
 * @property string $costprice
 * @property string $originalprice
 * @property integer $total
 * @property integer $totalcnf
 * @property string $sales
 * @property string $createtime
 * @property string $weight
 * @property integer $maxbuy
 * @property string $usermaxbuy
 * @property integer $hasoption
 * @property integer $dispatch
 * @property integer $isnew
 * @property integer $ishot
 * @property integer $isdiscount
 * @property integer $isrecommand
 * @property integer $istime
 * @property integer $timestart
 * @property integer $timeend
 * @property integer $viewcount
 */
class ShopGoods extends \yii\db\ActiveRecord
{


    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '商品',      //用于修改了什么的显示
        'mainfield' => 'title',      //主要的显示标题的字段
        'picturefield' => ['thumb','mutithumb'],        //图片字段
        'timefield' => ['timestart','timeend'],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];

    //0 拍下减库存 1 付款减库存 2 永久不减
    const TOTALCNF_PAI = 0;
    const TOTALCNF_PAY = 1;
    const TOTALCNF_NOREDUCE = 2;
    public static $totalcnfs = [
        self::TOTALCNF_PAI => '拍下减库存',
        self::TOTALCNF_PAY => '付款减库存',
        self::TOTALCNF_NOREDUCE => '永久不减'
    ];



    const STATUS_ACTIVE = 1;
    const STATUS_HIDDEN = 0;
    public static $statues = [
        self::STATUS_ACTIVE => '上架',
        self::STATUS_HIDDEN => '下架',

    ];

    const ISNEW = 1;
    const NONEW = 0;
    public static $isnew = [
        self::ISNEW => '新品',
        self::NONEW => '非新品',
    ];

    const RECOMMAND = 1;
    const NORECOMMAN= 0;
    public static $recommand = [
        self::NORECOMMAN => '不推荐',
        self::RECOMMAND => '推荐',

    ];

    const HOT = 1;
    const STANOHOT = 0;
    public static $ishot = [
        self::STANOHOT => '非热门',
        self::HOT => '热门',

    ];

    const TIME = 1;
    const NOTIME = 0;
    public static $istime = [
        self::NOTIME=> '非限时',
        self::TIME => '限时',

    ];

    const DISCOUNT = 1;
    const NODISCOUNT = 0;
    public static $isdiscount = [
        self::NODISCOUNT=> '非折扣',
        self::DISCOUNT => '折扣',
    ];

    const PREBUY = 1;
    const NOPREBUY = 0;
    public static $isprebuy = [
        self::NODISCOUNT=> '非预售',
        self::DISCOUNT => '预售',
    ];

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'shop_goods';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['cateid', 'status', 'displayorder','total', 'sales', 'createtime', 'maxbuy', 'usermaxbuy', 'hasoption', 'dispatch', 'isnew', 'ishot', 'isdiscount', 'isrecommand', 'istime', 'isprebuy', 'viewcount','pigid','isprebuy'], 'integer'],
            [['title','cateid','costprice','pigid','weight','total'], 'required'],
            [['content'], 'string'],
            [['marketprice', 'productprice', 'costprice', 'originalprice', 'weight'], 'number'],
            [['title','description'], 'string', 'max' => 100],
            [['unit'], 'string', 'max' => 5],
            [['goodssn'], 'string', 'max' => 50],
            [['thumb','mutithumb'], 'image', 'extensions' => 'jpg,jpeg,png','on'=>['uodate','insert']],
            [['timestart', 'timeend'],'safe']
        ];
    }



    public function scenarios()
    {
        $scenarios = parent::scenarios();//本行必填，不写的话就会报如上错误
        $scenarios['create'] =  ['mutithumb', 'thumb'];
        $scenarios['update'] = ['mutithumb', 'thumb'];

        return $scenarios;
    }


    public function beforeSave($insert)
    {
        if($this->isNewRecord){
            $this->createtime = time();
        }
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'cateid' => '分类',
            'pigid' => '追溯仔猪',
            'status' => '状态',
            'displayorder' => '排序',
            'title' => '名称',
            'thumb' => '缩略图',
            'mutithumb' => '图集',
            'unit' => '展示单位',
            'description' => '简述',
            'content' => '详细描述',
            'goodssn' => '商品sn',
            'marketprice' => '市场价',
            'productprice' => '售价',
            'costprice' => '成本价',
            'originalprice' => '原价',
            'total' => '总量',
            'totalcnf' => '减库存方式',
            'sales' => '已售数量',
            'createtime' => '创建时间',
            'weight' => '重量',
            'maxbuy' => '最多购买',
            'usermaxbuy' => '每人最多购买',
            'hasoption' => '是否有规格',
            'dispatch' => '配送方式',
            'isnew' => '是否新品',
            'ishot' => '是否热品',
            'isdiscount' => '是否打折',
            'isrecommand' => '是否推荐',
            'istime' => '是否限时',
            'timestart' => '限时开始',
            'timeend' => '限时结束',
            'viewcount' => '浏览量',
            'isprebuy'=>'是否预售'
        ];
    }


    public function getCate()
    {
        return $this->hasOne(ShopGoodsCategory::className(),['id'=>'cateid']);
    }


    /**
     * @return \yii\db\ActiveQuery
     * 追溯的仔猪
     */
    public function getPig()
    {
        return $this->hasOne(BusinessPig::className(),['id'=>'pigid']);
    }


    /**
     * 取出新品
     */
    public static function findNew($limit = 6)
    {
        $goods = self::find()->select('unit,id,weight,thumb as img,timestart,description as text,timeend,title as name,productprice as price,total as left')
            ->where('isnew=:isnew and status=:status',[':isnew'=>self::ISNEW,':status'=>ShopGoods::STATUS_ACTIVE])
            ->asArray()
            ->limit($limit)
            ->all();
        foreach ($goods  as $key => $row)
        {
            $goods[$key]['img'] = Upload::checkGetUrl($row['img']);
            $goods[$key]['url'] = Url::to(['/shop/goods/detail','gid'=>$row['id']]);
        }

        return $goods;

    }

    /**
     * 取出热卖
     */
    public static function findHot($limit = 6)
    {
        $goods = self::find()->select('unit,id,weight,thumb as img,description as text,timeend,title,productprice as price,total as nums,originalprice as o_price')
            ->where('ishot=:ishot and status=:status',[':ishot'=>self::HOT,':status'=>ShopGoods::STATUS_ACTIVE])
            ->asArray()
            ->limit($limit)
            ->all();
        foreach ($goods  as $key => $row)
        {
            $goods[$key]['img'] = Upload::checkGetUrl($row['img']);
            $goods[$key]['url'] = Url::to(['/shop/goods/detail','gid'=>$row['id']]);
        }

        return $goods;

    }


    /**
     * 取出限时购
     */
    public static function findTime($limit = 6)
    {
        $timegoods = ShopGoods::find()->select('unit,id,weight,thumb as img,timestart,description as text,timeend,title as name,productprice as price,total as left')
            ->where('istime=:time and status=:status and timeend>:timeend',[':time'=>ShopGoods::TIME,':status'=>ShopGoods::STATUS_ACTIVE,':timeend'=>time()])
            ->asArray()
            ->limit($limit)
            ->all();
        foreach ($timegoods  as $key => $row)
        {
            $timegoods[$key]['img'] = Upload::checkGetUrl($row['img']);
            $timegoods[$key]['url'] = Url::to(['/shop/goods/time-detail','gid'=>$row['id']]);
            $timegoods[$key]['timestart'] = date('Y-m-d H:i:s');
            $timegoods[$key]['timeend'] = date('Y-m-d H:i:s',$row['timeend']);
        }
        return $timegoods;

    }


    /**
     * @param int $limit
     * 取出推荐
     */
    public static function findCommand($limit = 6)
    {

        $goods = ShopGoods::find()->select('unit,id,weight,thumb as img,timestart,timeend,description as text,title as name,productprice as price,total as left')
            ->where('isrecommand=:iscommand and status=:status',[':iscommand'=>ShopGoods::RECOMMAND,':status'=>ShopGoods::STATUS_ACTIVE])
            ->asArray()
            ->limit($limit)
            ->all();
        foreach ($goods  as $key => $row)
        {
            $goods[$key]['img'] = Upload::checkGetUrl($row['img']);
            $goods[$key]['url'] = Url::to(['/shop/goods/detail','gid'=>$row['id']]);
        }

        return $goods;
    }


    /**
     * @param int $limit
     * @return mixed
     * 取出预售
     */
    public static function findPre($limit = 6)
    {

        $pregoods = ShopGoods::find()->select('unit,id,weight,thumb as img,title as name,description as text,total as `left`,productprice as price')
            ->where('isprebuy=:isprebuy and status=:status',[':isprebuy'=>ShopGoods::PREBUY,':status'=>ShopGoods::STATUS_ACTIVE])
            ->limit($limit)
            ->asArray()
            ->all();
        foreach ($pregoods as $key => $row)
        {
            $pregoods[$key]['img'] = Upload::checkGetUrl($row['img']);
            $pregoods[$key]['url'] = Url::to(['/shop/goods/detail','gid'=>$row['id']]);
        }

        return $pregoods;
    }





}
